package com.jiai.cloud.sys.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TGoods;
import com.jiai.cloud.sys.service.ITGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author
 * @since 2017-05-24
 */
@RestController
@RequestMapping("/api/v1/goods")
public class TGoodsController {

    @Autowired
    private ITGoodsService itGoodsService;
    //查询所得的

<<<<<<< Updated upstream


    //增加
    @RequestMapping(value = "/insert",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
=======
    //增
    @RequestMapping(value = "/addGoods",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
>>>>>>> Stashed changes
    public Object addGoods(@RequestBody TGoods tGoods)throws ServiceException {
        try{
            itGoodsService.insert(tGoods);
            return RestResultGenerator.genSuccessResult("插入成功");
        }catch (Exception e){
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }
<<<<<<< Updated upstream
    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itGoodsService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //通过ID删除
=======

    //删
>>>>>>> Stashed changes
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException{
        try{
            itGoodsService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
<<<<<<< Updated upstream
    //保存和更新
    @RequestMapping(value="/edit",method=RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
=======

    //查所有
       @RequestMapping(value = "/findByAll",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll() throws ServiceException{
        try {
            Page<TGoods> page = new Page<>(1, 20);
            itGoodsService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }


    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itGoodsService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
>>>>>>> Stashed changes
    @ResponseBody
    public Object edit( TGoods tGoods)throws ServiceException{
        try {
            itGoodsService.insertOrUpdate(tGoods);
<<<<<<< Updated upstream

=======
>>>>>>> Stashed changes
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

<<<<<<< Updated upstream



=======
>>>>>>> Stashed changes
}
